//
//  CacheDAO.m
//  CoreDataDemo
//
//  Created by chenguibang on 16/2/29.
//  Copyright © 2016年 chenguibang. All rights reserved.
//

#import "CacheDAO.h"
#import "RspCache+CoreDataProperties.h"
#import "RspCacheModel.h"
@implementation CacheDAO
-(NSMutableArray*)findAll{
    NSManagedObjectContext *cxt = [self managedObjectContext];
    NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@"RspCache" inManagedObjectContext:cxt];
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:entityDescription];
    NSError *error = nil;
    NSArray *listData = [cxt executeFetchRequest:request error:&error];
    NSMutableArray *resListData = [[NSMutableArray alloc]init];
    
    for ( RspCache  *rspCache in listData) {
        RspCacheModel *rsp =  [[RspCacheModel alloc]init];
        rsp.requrlstr = rspCache.requrlstr;
        rsp.rspdata = rspCache.rspdata;
        [resListData addObject:rsp];
        
    }
    return resListData;
    
}

-(RspCacheModel *)findById:(RspCacheModel *)model{
    NSManagedObjectContext *cxt = [self managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"RspCache" inManagedObjectContext:cxt];
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:entityDescription];
    
    NSPredicate *predicte = [NSPredicate predicateWithFormat:@""];
    [request setPredicate:predicte];
    
    NSError *error = nil;
    NSArray *listdata = [cxt executeFetchRequest:request error:&error];
    
    if ([listdata count]>0) {
        RspCache *ca = [listdata lastObject];
        RspCacheModel *rs = [[RspCacheModel alloc]init];
        rs.requrlstr = ca.requrlstr;
        rs.rspdata = ca.rspdata;
        return rs;
    }
    return nil;
    
    
}

-(BOOL)insert:(RspCacheModel *)model{
    NSManagedObjectContext *cxt = [self managedObjectContext];
    RspCache *insertmodel =(RspCache*)[NSEntityDescription insertNewObjectForEntityForName:@"RspCache" inManagedObjectContext:cxt];
    insertmodel.requrlstr = model.requrlstr;
    insertmodel.rspdata = model.rspdata;
    NSError *error = nil;
    if ([self.managedObjectContext save:&error]) {
        NSLog(@"数据插入成功");
        return YES;
    }else{
        NSLog(@"数据插入失败");
        return NO;
      
    }
    return NO;
    
}

-(BOOL)remove:(RspCacheModel *)model{
    NSManagedObjectContext *cxt = [self managedObjectContext];
    NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@"RspCache" inManagedObjectContext:cxt];
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:entityDescription];
    
    NSPredicate *predi = [NSPredicate predicateWithFormat:@"requrlstr = %@" ,model.requrlstr];
    request.predicate = predi;
    
    NSError *error = nil;
    NSArray *listData = [cxt executeFetchRequest:request error:&error];

    
    
    

    if ([listData count]>0) {
        RspCache *d = [listData lastObject];
        [cxt deleteObject:d];
        NSError *error = nil;
        if ([cxt save:&error]) {
            NSLog(@"删除成功");
            
            return YES;
        }else{
            NSLog(@"删除失败");
            return NO;
        }
    }else{
        NSLog(@"没有该条数据");
    }
    return NO;

}

-(BOOL)modify:(RspCacheModel *)model{
    
    NSManagedObjectContext *cxt = [self managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"RspCache" inManagedObjectContext:cxt];
    NSFetchRequest *request =[[NSFetchRequest alloc]init];
    request.entity = entityDescription;
    NSError *error = nil;
    
    //查出要次修改的数据
    NSArray *list = [cxt executeFetchRequest:request error:&error];
    
    if ([list count]>0) {
        RspCache *ca = [list lastObject];
        ca.requrlstr = model.requrlstr;
        ca.rspdata = model.rspdata;
        
        NSError *saerror = nil;
        if ([cxt save:&saerror]) {
            NSLog(@"修改成功");
        }else{
            NSLog(@"修改失败");
        }
        
        
    }else{
        NSLog(@"不存在该条数据");
    }
    
    
    
    return NO;
}
@end
